package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangmeng
 * @description 菜单控制器
 * @date 2019/9/18
 **/
@RestController
@RequestMapping("/checkmenu")
@Slf4j
public class CheckMenuController {

    @Reference
    private CheckGroupService checkGroupService;

    @Reference
    private CheckMenuService checkMenuService;

    /**
     * 添加
     *
     * @return
     */
    //DONE
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer[] roleIds) {
        //RPC请求添加
        checkMenuService.add(menu, roleIds);
        return new Result(true, "菜单添加成功");
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    //DONEDONEDONEDONEDONE 分页
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        //rpc查询结果
        PageResult pageResult = checkMenuService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,"查询成功",pageResult);

    }

    /**
     * 根据ID查询
     *
     * @param menuId
     * @return
     */
    //done
    @RequestMapping("/findById")
    public Result findById(Integer menuId) {
        //rpc 调用业务处理
        Menu menu = checkMenuService.findById(menuId);
        log.info("GNIHASHND:{}",menu.toString());
        //封装返回值
        return new Result(true,"查询成功", menu);

    }

    /**
     * 根据检查组ID查询关联的检查项ID集和
     *
     * @param id
     * @return
     */
    //done
    @RequestMapping("/findCheckItemIdsByMenuId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        //rpc 调用业务处理
        List<Integer> roleIds = checkMenuService.findCheckItemIdsByCheckGroupId(id);
        //封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, roleIds);

    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu, Integer[] roleIds) {
        //RPC请求添加
        checkMenuService.edit(menu, roleIds);
        return new Result(true, "编辑菜单成功");

    }

    /**
     * 查询所有
     *
     * @return
     */
    //DONE find all roles
    @RequestMapping("findAll")
    public Result findAll() {
        //rpc调用查询数据
        List<Role> roles = checkMenuService.findAll();
        //封装返回结果
        return new Result(true, "查询成功", roles);
    }

    //done
    @RequestMapping("delete")
    public Result delete(Integer menuId) {
        log.info("SANBAFSBJSA{}",menuId);
        //rpc调用查询数据
        checkMenuService.delete(menuId);
        //封装返回结果
        return new Result(true, "删除菜单成功");
    }
}
